var form;
var $;
var table;
layui.use(['form','jquery','table'],function() {
    form = layui.form;
    $ = layui.jquery;
    table = layui.table;

    //走请求查数据
    getAllCourseRecordData('', '', '', '');
});

function getAllCourseRecordData(result, artcleTitle, createDate, createId){
    if (result == 1){
        $('.sele2').addClass('selectStyle');
        $('.sele1').removeClass('selectStyle');
        $('.sele3').removeClass('selectStyle');
        $('.sele4').removeClass('selectStyle');
    } else if (result == 2){
        $('.sele3').addClass('selectStyle');
        $('.sele1').removeClass('selectStyle');
        $('.sele2').removeClass('selectStyle');
        $('.sele4').removeClass('selectStyle');
    } else if (result == 3){
        $('.sele4').addClass('selectStyle');
        $('.sele1').removeClass('selectStyle');
        $('.sele2').removeClass('selectStyle');
        $('.sele3').removeClass('selectStyle');
    }else if (result == ''){
        $('.sele1').addClass('selectStyle');
        $('.sele2').removeClass('selectStyle');
        $('.sele3').removeClass('selectStyle');
        $('.sele4').removeClass('selectStyle')
    }else {;
    }

    $.ajax({
        url: '/CourseRecord/getAllMicroCircle',
        type: 'post',
        dataType: 'json',
        data: {
            'result': result,
            'artcleTitle': artcleTitle,
            'createDate': createDate,
            'userId': createId
        },
        success:function (item) {
            tab(item.data);
        }
    });
};

//展示表
function tab(data) {
    table.render({
        elem: '#demo' ,
        toolbar:'#toolutil',
        data: data,
        cols:[[
            {title:'序号', align:'center', templet:function (data) {
                    return data.LAY_INDEX;
                }},
            {field:'serialNumber',title:'微圈编号', align:'center'},
            {field:'artcleUrl',title:'微圈图片',align:'center', templet:function(data){
                    return "<img src=' " + data.artcleUrl  + " '>";
                }},
            {field:'artcleTitle',title:'微圈名称',align:'center'},
            {field:'createName',title:'用户昵称',align:'center'},
            {field:'categroyName',title:'微圈分类',align:'center'},
            {field:'result',title:'审核状态',align:'center', templet:function (data) {
                    if (data.result == 1){
                        return '审核通过';
                    }else if (data.result == 2){
                        return '待审核';
                    }else if (data.result == 3) {
                        return '审核驳回';
                    }
                }},
            {title:'操作',align:'center', templet:function(data){
                    if (data.result == 1){
                        return "<a href='JavaScript:selectMicroCircle(\"" + data.microcircleId +  "\");' class='operation'>查看</a>";
                    } else if (data.result == 2){
                        return "<a href='JavaScript:selectMicroCircle(\"" + data.microcircleId +  "\");' class='operation'>查看</a>" + '&nbsp;&nbsp;'
                            +"<a class='operation' href='javascript:judge(\"" + data.toExamineId + "\", \"" + data.artcleTitle + "\");'>审核</a>";
                    } else if (data.result == 3) {
                        return "<a href='JavaScript:selectMicroCircle(\"" + data.microcircleId +  "\");' class='operation'>查看</a>";
                    }
                }},
        ]]
    });
};


//刷新
function reload() {
    location.reload();
}

//搜索
function search() {
    var artcleTitle = $('input[name=artcleTitle]').val();
    var createDate = $('input[name=createDate]').val();
    var createName = $('input[name=createName]').val();
    var createId;
    if (createName != null && createName != ''){
        $.ajax({
            url: '/User/getByName',
            type: 'post',
            data: {'createName': createName},
            dataType: 'json',
            async: false,
            success:function (item) {
                createId = item.data;
            }
        });
        if (createId == null){
            createId = createName;
        }
    }
    getAllCourseRecordData('', artcleTitle, createDate, createId);
};

//查看微圈信息
function  selectMicroCircle(id) {
    $('#proId').val(id);
    layer.open({
        type:2,
        content:"/toexamine/microCircle/selectMicroCircle.html",
        area:['1020px','500px'],
        end:function () {

        }
    });
}

//审核
function judge(id, name) {
    $('#proId').val(id);
    $('#proName').val(name);
    var implementId = JSON.parse(localStorage.getItem('user')).userId;
    layer.open({
        type:2,
        content:"/toexamine/microCircle/toExamine.html",
        area:['540px','460px'],
        btn:['确定', '取消'],
        yes: function (index, layero) {
            var form = layero.find('iframe').contents();
            var id = form.find('#id').text();
            var result = form.find('input:checked').val();
            var remarks = form.find('textarea[name=remarks]').val();
            console.log(id)
            console.log(result)
            console.log(remarks);
            $.ajax({
                url: '/CourseRecord/updateCourseRecord',
                type: 'post',
                dataType: 'json',
                data: {
                    'id': id,
                    'result': result,
                    'remarks': remarks,
                    'implementId': implementId,
                },
                success:function (item) {
                    if (item.data){
                        layer.msg('审核成功');
                        getAllCourseRecordData('','', '', '');
                        if (result == 1){
                            result = 2
                        } else if (result == 3){
                            result = 1
                        }
                        addAuditLog(result, remarks);
                    } else{
                        layer.msg('审核失败');
                    }
                }
            })
            layer.close(index);
        },
        btn2: function (index, layero) {
            layer.close(index);
        }
    });
}


//添加审核信息
function addAuditLog(result, remarks) {
    var id = JSON.parse(localStorage.getItem('user')).userId;
    //截取时间
    var date = new Date().toLocaleString('zh', { hour12: false }).split('/').join('-');
    var temp = date.substring(8, -date.lastIndexOf('-') + 7);
    date = date.substr(date.lastIndexOf('-') + 1);
    date = temp + date;
    $.ajax({
        url: '/AuditLog/addAuditLog',
        type: 'post',
        dataType: 'json',
        data: {
            'tableName': '4',
            'type': result,
            'userId': id,
            'createDate': date,
            'remark': remarks
        }
    })
}

//个数
getNum();
function getNum() {
    $.ajax({
        url: '/CourseRecord/getAllNum',
        type: 'post',
        dataType: 'json',
        data: {'type': 4},
        success:function (item) {
            $('.sele1 span').text(item.data);
        }
    });
    $.ajax({
        url: '/CourseRecord/getNumYes',
        type: 'post',
        dataType: 'json',
        data: {'type': 4},
        success:function (item) {
            $('.sele2 span').text(item.data);
        }
    });
    $.ajax({
        url: '/CourseRecord/getNum',
        type: 'post',
        dataType: 'json',
        data: {'type': 4},
        success:function (item) {
            $('.sele3 span').text(item.data);
        }
    });
    $.ajax({
        url: '/CourseRecord/getNumNo',
        type: 'post',
        dataType: 'json',
        data: {'type': 4},
        success:function (item) {
            $('.sele4 span').text(item.data);
        }
    });
}

function backOff() {
    var arr = localStorage.getItem("numArr");
    var numArr = arr.split(",");
    numArr.pop();
    window.parent.layui.element.tabChange('tab',numArr[numArr.length - 1]);
    localStorage.setItem("numArr",numArr);
};